package br.com.sgdc.view.action;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import br.com.sgdc.Facade;
import br.com.sgdc.model.bean.cloud.DropboxCloud;
import br.com.sgdc.view.WindowCadastroClouds;
import br.com.sgdc.view.util.TipoCloud;

public class ActionWindowCadastroClouds implements WindowListener {
	private WindowCadastroClouds windowCadastroClouds;
	private Facade facade;

	public ActionWindowCadastroClouds(WindowCadastroClouds windowCadastroClouds) {
		this.windowCadastroClouds = windowCadastroClouds;
		facade = new Facade();
	}

	@Override
	public void windowActivated(WindowEvent arg0) {

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowClosing(WindowEvent arg0) {

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {

	}

	@Override
	public void windowIconified(WindowEvent arg0) {

	}

	@Override
	public void windowOpened(WindowEvent event) {
		if (event.getSource() == windowCadastroClouds.window) {
			try {
				facade.setClouds(facade.getCloudDao().listClouds());
				for (int i = 0; i < facade.getClouds().size(); i++) {
					String[] str = new String[3];
					str[0] = String.valueOf(facade.getClouds().get(i).getId());
					if(facade.getClouds().get(i) instanceof DropboxCloud){
						str[1] = TipoCloud.DROPBOX.toString();	
					} else {
						str[1] = TipoCloud.GOOGLE_DRIVE.toString();
					}

					str[2] = String.valueOf(facade.getClouds().get(i).getNomeCloud());

					windowCadastroClouds.getModelo().addRow(str);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
